package com.ds.client.actors;

import org.lwjgl.Sys;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.ds.client.main.Main;

public class Car extends Actor {
	public static final String CAR1 = "res/Car1.pack";
	public static final String CAR2 = "res/Car2.pack";
	public static final int START_X_CAR1 = 480;
	public static final int START_X_CAR2 = 570;
	private float speed = 1.0f;
	private float x;
	private float y;
	//a variable for 1 time explode & music
	private int explodeTime;
	float stateTime;
	private Vector3f newPos = new Vector3f(0f, 0f, 0f);

	public Boolean moving = false;
	private int currentIndex;
	TextureAtlas atlas;
	TextureAtlas colAtlas;
	TextureRegion walksFrame;
	Animation animation;

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public Car(String path, int startX, String name) {
		super(name);
		atlas = new TextureAtlas(Gdx.files.internal(path));
		colAtlas = new TextureAtlas(Gdx.files.internal("res/bomb.pack"));
		this.height = 50f;
		this.width = 50f;
		x = startX;
		y = (Main.GameHight - this.height) / 2;
		setCurrentIndex(1);
		speed = 1.0f;
		this.moving = true;
		explodeTime = 1;
	}

	public static Car getCar1() {
		return new Car(CAR1, START_X_CAR1, "Car1");

	}

	public static Car getCar2() {
		return new Car(CAR2, START_X_CAR2, "Car2");

	}

	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		TextureRegion currentFrame;
		stateTime += Gdx.graphics.getDeltaTime(); 
		
		if(moving) {
			currentFrame = atlas.findRegion(currentIndex + "");		
		}else{
			showCollision();
			currentFrame = animation.getKeyFrame(stateTime, true); 
		}
//		if(speed != 0){
//			running.
//		}
		batch.draw(currentFrame, this.x, this.y);

	}
	
	public void showCollision(){
		TextureRegion[] walksFrame;
		walksFrame = new TextureRegion[4];
		walksFrame[0] = colAtlas.findRegion("bomb0");
		walksFrame[1] = colAtlas.findRegion("bomb1");
		walksFrame[2] = colAtlas.findRegion("bomb2");
		walksFrame[3] = colAtlas.findRegion("bomb3");
		animation = new Animation(0.25f, walksFrame);
	}

	/**
	 * get a new position(for rmi use)
	 * 
	 * @param x
	 * @param y
	 */
	public void updatePos(Vector3f newPos) {
		setX(newPos.x);
		setY(newPos.y);
		currentIndex = (int) newPos.z;
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		if (x > 0 && y > 0 && this.height > y && this.width > x) {
			this.x += 1;
			this.y += 1;
			this.parent.findActor("bg").x -= 1;
			return this;
		} else {
			return null;
		}
	}

	public void goForward() {
		calPos("forward");
		// this.parent.findActor("bg").x -= speed;
	}

	public void goBackward() {
		calPos("back");
		/*
		 * moving = true; float speed = 0.5f; this.x -= speed;
		 */
		// this.parent.findActor("bg").x += speed;
	}

	public void leftRotate() {
		calPos("left");
	}

	public void rightRotate() {
		calPos("right");
	}
	
	

	/**
	 * This function updates the car's new position
	 * 
	 * @param pos
	 *            (x: x increment, y: y increment, z: new image index)
	 * @return
	 */
	public void calPos(String pos) {
		if (pos.equals("left") && this.moving == true) {
			//try {
				if (speed > 0) {
					//Thread.sleep(100);
					if (currentIndex > 1) {
						currentIndex--;
					} else {
						currentIndex = 16;
					}
					setNewPos(0, 0, currentIndex);
					//carMove(speed);
					
				}
			/*} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();*/
			//}

		} else if (pos.equals("right") && this.moving == true) {
//			try {
				if (speed > 0) {
					//Thread.sleep(100);

					if (currentIndex < 16) {
						currentIndex++;
					} else {
						currentIndex = 1;
					}
					setNewPos(0, 0, currentIndex);
					//carMove(speed);
				}
			/*} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		} else if (pos.equals("forward") && this.moving == true) {
			carMove(speed);
			if (speed < 30f)
				speed += 0.05f;
		} else {
			if (this.moving == true) {
				carMove(speed);
				speed = (speed > 0) ? speed - 0.1f : 0f;
			} else {
				speed = 0f;
				carMove(speed);
			}
		}
	}

	public void carMove(float speed) {
		int angle = index2Angle();
		double degrees = 90 - angle * 22.5;
		double radians = Math.toRadians(degrees);
		float xIncrement = (float) (speed * Math.cos(radians));
		float yIncrement = (float) (speed * Math.sin(radians));
		this.x += xIncrement;
		this.y += yIncrement;
		setNewPos(xIncrement, yIncrement, currentIndex);

	}

	/**
	 * Transfer image index to angle index
	 * @return
	 */
	public int index2Angle() {
		return currentIndex - 1;
	}

	public void stand() {
		if (this.moving == true) {
			if (speed > 0) {
				speed -= 0.1f;
				carMove(speed);
			} else {
				speed = 0;
				carMove(speed);
			}
		} else {
			speed = 0;
			carMove(speed);
		}
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public Vector3f getNewPos() {
		return newPos;
	}

	public void setNewPos(float x, float y, float z) {
		this.newPos = new Vector3f(x, y, z);
	}
	public int getExplodeTime(){
		return this.explodeTime;
	}
	public void setExplodeTime(){
		this.explodeTime -= 1;
	}
	public float getSpeed(){
		return this.speed;
	}
}
